from django.http import Http404
from django.views.generic import DetailView
from apps.blog.models import Article


class BlogDetail(DetailView):
    template_name = 'blog/detail.html'

    def get_object(self, queryset=None):
        article = Article.gql("WHERE slug = :1", self.kwargs.get('slug')).get()

        if not article.is_published:
            raise Http404

        return article

    def get_context_data(self, **kwargs):
        articles = Article.all().filter('is_published', True).order(
            '-updated_at')
        data = super(BlogDetail, self).get_context_data(**kwargs)
        data.update({'articles': articles})

        return data
